package xdu.lz.stage6_threadpool.chapter2_excutors_threadPoolImpl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created at 2019/10/24 0024 上午 11:31
 * 测试线程池的工作机制。
 * execute task -> put into queue  <-  thread get the task to run
 * <p>
 * Executors.newFixedThreadPool(5)时候
 * 1.若不提交runnable，则不会创建线程，也就是懒加载。
 * 2.当提交的任务小于等于5，提交几个会创建几个，并且空闲不会销毁
 * 下面的API是让coreThread强制创建
 * 每次增加1个，直到coreSize
 * executor.prestartCoreThread();
 * 直接增加到coreSize
 * executor.prestartAllCoreThreads();
 * 3.可以设置coreSize在idel时候自动销毁 ，然后关闭线程池
 * <p>
 * 其他API：
 * remove(Runnable task): 丢弃queue中指定的task
 * getCompletedTaskCount():completedCount
 * getTaskCount(): completedCount + queueSize
 *
 * protected继承可见,相当于某个任务的切面，在任务执行前后执行，执行后可以拿到异常
 * protected void beforeExecute(Thread t, Runnable r) { }
 * protected void afterExecute(Runnable r, Throwable t) { }
 * <p>
 * <p>
 */
public class TestThreadPoolWorkMechanism {

    public static void main(String[] args) {
        testPool1();
//        testPool2();
//        testPool3();
    }

    //直接结束，不会创建线程 ，懒加载
    private static void testPool1() {
        ThreadPoolExecutor service = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        //即使在queue中添加了任务，但是由于没有execute触发工作机制 ，也不会创建线程
        service.getQueue().add(()->{});

    }

    //当任务达到coreSize前，来一个任务创建一个线程，懒加载。并且默认不会回收coreSize
    private static void testPool2() {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        executor.setKeepAliveTime(1, TimeUnit.SECONDS);
        executor.allowCoreThreadTimeOut(true);
        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

    }

    //当没有任务时，手动让线程池创建coreSize个核心线程
    private static void testPool3() {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);

        //每次增加1个，直到coreSize
        executor.prestartCoreThread();
        //直接增加到coreSize
        executor.prestartAllCoreThreads();

    }

    //当任务执行结束后，设定回收coreSize，自动关闭线程池
    private static ExecutorService newFixedThreadPool4() {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        //这个参数必须设定大于0
        executor.setKeepAliveTime(1, TimeUnit.SECONDS);
        executor.allowCoreThreadTimeOut(true);
        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        executor.submit(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        return executor;
    }


}
